package controler;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import castleContent.User;
import castleContent.Castle;
import dataBase.DataBase;
import dataBase.UpdateDBTables;

@WebServlet("/CreateSoldierServlet")
public class CreateSoldierServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		String warriorType = request.getParameter("warriorType");
		User user = (User) session.getAttribute("Object");
		String username = user.getName();
		Castle castle = (Castle) session.getAttribute("Castle");

		if (castle.getBarracks().getPresentArmy() < castle.getBarracks()
				.getArmyCapacity()) {
			if (warriorType.equals("Barbarian")) {
				if (castle.getFoodFarm().getAvailableResource() >= 150) {
					castle.crateBarbarian();
					updateUsersFoodInDateBaseBarbarian(response, user, username);
				}
			} else if (warriorType.equals("Archer")) {
				if (castle.getFoodFarm().getAvailableResource() >= 300) {
					castle.crateAcher();
					updateUsersFoodInDateBaseArcher(response, user, username);
				}
			} else {
				if (castle.getFoodFarm().getAvailableResource() >= 500) {
					castle.crateWizzard();
					updateUsersFoodInDateBaseWizard(response, user, username);
				}
			}
			printResourceAndArmyCapacity(response, user, username);
		}
	}

	private void updateUsersFoodInDateBaseBarbarian(
			HttpServletResponse response, User user, String username)
			throws IOException {
		try {
			UpdateDBTables.updateFoodOfBarbarian(DataBase.getDbCon(), username);
			UpdateDBTables.updateBarbarianCount(DataBase.getDbCon(), username);
		} catch (SQLException | ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	private void updateUsersFoodInDateBaseArcher(HttpServletResponse response,
			User user, String username) throws IOException {
		try {
			UpdateDBTables.updateFoodOfArcher(DataBase.getDbCon(), username);
			UpdateDBTables.updateArcherCount(DataBase.getDbCon(), username);
		} catch (SQLException | ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	private void updateUsersFoodInDateBaseWizard(HttpServletResponse response,
			User user, String username) throws IOException {
		try {
			UpdateDBTables.updateFoodOfWizard(DataBase.getDbCon(), username);
			UpdateDBTables.updateWizardCount(DataBase.getDbCon(), username);
		} catch (SQLException | ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	private void printResourceAndArmyCapacity(HttpServletResponse response,
			User user, String username) throws IOException {
		response.getWriter().print(
				"Food : " + user.castle.getFoodFarm().getAvailableResource()
						+ "	Gold : "
						+ user.castle.getGoldMine().getAvailableResource()
						+ "<br>Army : "
						+ user.castle.getBarracks().getPresentArmy() + " / "
						+ user.castle.getBarracks().getArmyCapacity() + "<br>"
						+ "Barbarians : " + user.castle.getBarracks().getBarbarians() 
						+ " Archers : " + user.castle.getBarracks().getArchers()
						+ " Wizards : " + user.castle.getBarracks().getWizzards());
	}
}
